#!/bin/bash

REMOTE_ADDR=172.18.180.228

REMOTEDIR=/home/work/changsha_project/basic_data
DATADIR=/home/dig/graph_data/changsha_data

#HIVE_DATA_DIR=/mnt/changsha/all_json_data_tmp

cd ${DATADIR}
for DT in `ls | grep 'data_parsed_'`
do
#  DT="data_parsed_`date -d "$i days ago" +%Y%m%d`_23"

  if [ ! -d ${DATADIR}/${DT} ]; then
      echo "当前文件不是目录: ${DT}"
      continue
  fi

  cd ${DATADIR}
  echo "开始压缩数据: ${DT}"
  tar -zcf ${DT}.tgz ${DT}

#  # 复制数据到hive ETL需要的目录
#  cp -rf ${DT}.tgz ${HIVE_DATA_DIR}

  # 计算MD5
  cs_md5=`cat ${DT}.tgz | md5sum | awk '{print $1}'`

  try_cnt=0
  while [ ${try_cnt} -lt 4 ];
  do
       echo "开始传输数据: ${DT}.tgz"
       # 先删除已经存在的数据
       ssh work@${REMOTE_ADDR} rm -rf ${REMOTEDIR}/${DT}.tgz
       rsync --append --partial --progress -e "ssh -p22" ${DT}.tgz work@${REMOTE_ADDR}:${REMOTEDIR}/
       # result=`ssh work@${REMOTE_ADDR} "sh" ${REMOTEDIR}"/cat_cs_md5.sh"`
       echo "开始计算MD5: ${DT}.tgz"
       result=`ssh work@${REMOTE_ADDR} cat ${REMOTEDIR}/${DT}.tgz | md5sum | awk '{print $1}'`

       if [ "$cs_md5" != "$result" ];then
           echo "error md5 不一致: ${DT}  result = ${result} cs_md5 = ${cs_md5}"
           try_cnt=`expr ${try_cnt} + 1`
       else
           echo " ${DT}.tgz md5一致: result = ${result} cs_md5 = ${cs_md5}"
           ssh work@${REMOTE_ADDR} "echo ${cs_md5} > ${REMOTEDIR}/${DT}.md5"
           break
       fi
  done

  if [ ${try_cnt} -lt 4 ]; then
      rm -rf ${DT}.tgz_bak
      mv ${DT}.tgz ${DT}.tgz_bak
      rm -rf ${DT}
      echo "数据传输并删除成功: ${DT} "
  fi
done

echo "上传本地服务端 basic数据完成.."

# 添加一个删除历史数据逻辑  游丰 2018-11-05
for ((i=10;i<=17;i++));
do
  DT="data_parsed_`date -d "$i days ago" +%Y%m%d`_23"
  if [ -f "${DATADIR}/${DT}.tgz_bak" ];then
    rm -rf ${DATADIR}/${DT}.tgz_bak
    echo "删除历史数据: ${DT}.tgz_bak"
  fi

  if [ -d "${DATADIR}/${DT}" ];then
    rm -rf ${DATADIR}/${DT}
    echo "删除历史数据: ${DT}"
  fi

done

echo "删除本地服务端 basic历史数据完成.."


ssh work@${REMOTE_ADDR} sh ${REMOTEDIR}/upload_basic_data_to_changsha_with_date.sh

echo "basic 远程过程调用完成..."